{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 一、列表\n",
    "## 1.1列表的表示\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#直接表示\n",
    "ls = [\"python\",1989,True,10.0]\n",
    "\n",
    "#使用list()函数\n",
    "#字符串转列表\n",
    "list(\"dw123\")\n",
    "\n",
    "#元组转列表\n",
    "list((1,2,3))\n",
    "\n",
    "#字典转列表\n",
    "list({\"name\": \"张三\", \"age\": 18})\n",
    "\n",
    "#集合转列表\n",
    "list({1,2,3})\n",
    "\n",
    "#特殊的range rang(起始数据,结束数据,数字间隔)\n",
    "for i in [1,2,3,4,5]:\n",
    "    print(i)\n",
    "\n",
    "for i in range(6):\n",
    "    print(i)\n",
    "\n",
    "#range转列表\n",
    "a=list(range(10))\n",
    "print(a)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-18T14:13:23.794309700Z",
     "start_time": "2024-11-18T14:13:23.764311400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2 列表的性质"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "'python'"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表长度\n",
    "print(len(ls))\n",
    "\n",
    "#索引---与字符串规则完全相同\n",
    "ls[0]\n",
    "a = [\"python\",1989,True,10.0]\n",
    "print(a[1])\n",
    "print(a[-1])\n",
    "\n",
    "#切片\n",
    "#正向切片\n",
    "print( a[0:3])\n",
    "\n",
    "#反向切片\n",
    "print( a[-3:-1])\n",
    "\n",
    "#步长\n",
    "print( a[0:3:2])\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-18T14:17:17.272610700Z",
     "start_time": "2024-11-18T14:17:17.205993200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.3 列表的操作符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 1989, True, 10.0, 1, 2, 3, 4, 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 1.3.1 列表的拼接\n",
    "a = [\"python\",1989,True,10.0]\n",
    "b = [1,2,3,4,5]\n",
    "c = a + b\n",
    "print(c)\n",
    "\n",
    "## 1.3.2 列表的复制，可以用来初始化列表\n",
    "[0]*10\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T07:32:02.885680200Z",
     "start_time": "2024-11-20T07:32:02.789622600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.4 列表的操作方法\n",
    "### 1.4.1增加元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'c', 'c++', 'java']\n",
      "['python', 'c#', 'c', 'c++', 'java']\n",
      "['python', 'c#', 'c', 'c++', 'java', ['c', 'c++']]\n",
      "['python', 'c#', 'c', 'c++', 'java', ['c', 'c++'], 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "#在末尾增加元素\n",
    "language = [\"python\",\"c\",\"c++\"]\n",
    "language.append(\"java\")\n",
    "print(language)\n",
    "#在指定位置增加元素\n",
    "language.insert(1,\"c#\")\n",
    "print(language)\n",
    "\n",
    "##将整个列表插入末尾(会有“【】”)\n",
    "language.append([\"c\",\"c++\"])\n",
    "print(language)\n",
    "language.extend([1,2,3])\n",
    "print(language)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T07:38:38.384424200Z",
     "start_time": "2024-11-20T07:38:38.364697300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.2删除元素\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#删除指定位置的元素\n",
    "language.pop(0)\n",
    "print(language)\n",
    "#不写参数默认删除最后一个元素\n",
    "language.pop()\n",
    "print(language)\n",
    "\n",
    "#删除指定名称的元素\n",
    "language.remove(\"c++\")\n",
    "print(language)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.3查找元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'c#', 'c', 'c++', 'java', ['c', 'c++'], 1, 2, 3]\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(language)\n",
    "index = language.index(\"c#\")\n",
    "print(index)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T07:43:01.548097800Z",
     "start_time": "2024-11-20T07:43:01.524170500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.4 修改元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['c', 'c#', 'c', 'c++', 'java', ['c', 'c++'], 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "language[0] = \"c\"\n",
    "print(language)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T07:44:04.991894500Z",
     "start_time": "2024-11-20T07:44:04.968892400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.5 列表的复制"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['c#', 'c++', 'java', ['c', 'c++'], 1, 2, 3]\n",
      "['c#', 'c++', 'java', ['c', 'c++'], 1, 2, 3]\n",
      "['c#', 'c++', 'java', ['c', 'c++'], 2, 3]\n",
      "['c#', 'c++', 'java', ['c', 'c++'], 2, 3]\n",
      "['c#', 'c++', 'java', ['c', 'c++'], 2, 3, 1]\n",
      "['c#', 'c++', 'java', ['c', 'c++'], 2, 3, 1]\n",
      "['c#', 'java', ['c', 'c++'], 2, 3, 1]\n",
      "['c#', 'c++', 'java', ['c', 'c++'], 2, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "## 错误方式（给列表起别名）\n",
    "a = language\n",
    "print(a)\n",
    "print(language)\n",
    "language.remove(1)\n",
    "print(language)\n",
    "print(a)\n",
    "language.append(1)\n",
    "##可以发现只是给language起了一个别名，但是a的值依然改变\n",
    "# 正确方式（浅拷贝）\n",
    "#方法一：copy\n",
    "a = language.copy()\n",
    "print(a)\n",
    "print(language)\n",
    "language.remove(\"c++\")\n",
    "print(language)\n",
    "print(a)\n",
    "##可以发现只是给language起了一个别名，但是a的值依然改变"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T07:53:53.179549700Z",
     "start_time": "2024-11-20T07:53:53.153499100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.6 列表的排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[5, 4, 3, 2, 1]\n",
      "[1, 2, 3, 4, 5]\n",
      "[5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "## sort，直接在原列表上排序\n",
    "a = [1,2,3,4,5]\n",
    "a.sort()\n",
    "print(a)\n",
    "a.sort(reverse=True)\n",
    "print(a)\n",
    "\n",
    "## sorted,原列表不变，返回一个新列表\n",
    "a = [1,2,3,4,5]\n",
    "b = sorted(a)\n",
    "print(b)\n",
    "b = sorted(a,reverse=True)\n",
    "print(b)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T07:56:32.807150700Z",
     "start_time": "2024-11-20T07:56:32.791106500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.7 列表的反转"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "ls = [1,2,3,4,5]\n",
    "ls.reverse()\n",
    "print(ls)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二、元组\n",
    "## 2.1元组的表达"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "names = (\"python\",\"c\",\"c++\")\n",
    "print(names)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2元组的操作\n",
    "不支持元素的增加、删除、修改操作，其他操作和列表类似"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3元组的常见用处"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 4, 9)\n",
      "<class 'tuple'>\n",
      "1 4 9\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "#元素打包返回\n",
    "def func(a,b,c):\n",
    "    return a**2,b**2,c**2\n",
    "print(func(1,2,3))\n",
    "print(type(func(1,2,3)))\n",
    "\n",
    "#元素解包赋值\n",
    "a,b,c = func(1,2,3)\n",
    "print(a,b,c)\n",
    "#"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T08:05:02.058684700Z",
     "start_time": "2024-11-20T08:05:02.044540100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 三、字典\n",
    "## 3.1字典的表示（数据是无序的，只能通过key查找）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = {\"name\": \"张三\", \"age\": 18}\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2字典的要求\n",
    "1.字典的键不能重复\n",
    "2.字典的键必须是不可变的，如字符串，数字，元组"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2字典的性质"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "dict_keys(['name', 'age', 'score'])\n",
      "dict_values(['张三', 18, 100])\n",
      "dict_items([('name', '张三'), ('age', 18), ('score', 100)])\n"
     ]
    }
   ],
   "source": [
    "a = {\"name\": \"张三\", \"age\": 18, \"score\": 100}\n",
    "#字典的长度\n",
    "print(len(a))\n",
    "#字典的键\n",
    "print(a.keys())\n",
    "#字典的值\n",
    "print(a.values())\n",
    "#字典的键值对\n",
    "print(a.items())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T08:12:53.500196Z",
     "start_time": "2024-11-20T08:12:53.485915600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.3字典的操作\n",
    "### 3.3.1 增加和删除"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{20110001: '张三', 20110002: '李四', 20110003: '王五', 20110004: '赵六'}\n",
      "{20110001: '张三', 20110003: '王五', 20110004: '赵六'}\n"
     ]
    }
   ],
   "source": [
    "students = {20110001: \"张三\", 20110002: \"李四\", 20110003: \"王五\"}\n",
    "\n",
    "#增加键值对\n",
    "students[20110004] = \"赵六\"\n",
    "print(students)\n",
    "#删除键值对\n",
    "del students[20110002]\n",
    "print(students)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T08:14:51.454117500Z",
     "start_time": "2024-11-20T08:14:51.424904200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.3.2 修改"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '李四', 'age': 18, 'score': 100}\n"
     ]
    }
   ],
   "source": [
    "#修改字典的值\n",
    "a = {\"name\": \"张三\", \"age\": 18, \"score\": 100}\n",
    "a[\"name\"] = \"李四\"\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T08:15:53.695492200Z",
     "start_time": "2024-11-20T08:15:53.671310900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.3.3 d.get()方法\n",
    "从字典中获取键key对应的值，如果key不存在，返回None\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#例子：统计“牛奶奶找刘奶奶买牛奶”中字符出现的频率\n",
    "a = \"牛奶奶找刘奶奶买牛奶\"\n",
    "b = {}\n",
    "for i in a:\n",
    "    b[i] = b.get(i,0)+1#如果在b中找到i，说明i出现过，就返回b[i]的值，如果找不到i，说明没出现过，就返回0\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 四、集合\n",
    "## 4.1集合的表示"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "a = {1,2,3,4,5}\n",
    "print(a)\n",
    "#集合的特点：无序，不重复\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.2集合的运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{4, 5}\n",
      "{1, 2, 3, 4, 5, 6, 7, 8}\n",
      "{1, 2, 3}\n",
      "{1, 2, 3, 6, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "a = {1,2,3,4,5}\n",
    "b = {4,5,6,7,8}\n",
    "#交集\n",
    "print(a&b)\n",
    "#并集\n",
    "print(a|b)\n",
    "#差集\n",
    "print(a-b)\n",
    "#对称差，返回两个结合非交集的元素\n",
    "print(a^b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T09:02:05.118635800Z",
     "start_time": "2024-11-20T09:02:05.049921900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.3集合的操作\n",
    "### 4.3.1 增加和删除"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6}\n",
      "{1, 2, 3, 4, 5}\n"
     ]
    }
   ],
   "source": [
    "a = {1,2,3,4,5}\n",
    "a.add(6)\n",
    "print(a)\n",
    "a.remove(6)\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T09:03:43.817442900Z",
     "start_time": "2024-11-20T09:03:43.798406100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.3.2 集合的长度"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(len(a))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T09:03:45.572392800Z",
     "start_time": "2024-11-20T09:03:45.557240Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
